// pages/message/message.js

const app = getApp();
let api = require('../../utils/api.js');
Page({

  /**
   * 页面的初始数据
   */
  data: {
    messageList: [],
    currentIndex: 0,
    titleTop: '', //区域离顶部的高度
    scrollTop: 0, //滑动条离顶部的距离
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var that = this;
    app.getRemind();
    wx.getSystemInfo({
      success: function (res) {
        that.setData({
          winWidth: res.windowWidth,
          winHeight: res.windowHeight - app.globalData.navHeight,
          currentIndex: 0
        });
      }
    });
    var token = wx.getStorageSync('TOKEN');
    that.getMessgaeList(1, token);
  },
  titleClick: function (e) {
    let that = this;
    var orderList = []

    var token = wx.getStorageSync('TOKEN');
    if (e.currentTarget.dataset.idx == 0) {
      that.getMessgaeList(1, token);
    } else if (e.currentTarget.dataset.idx == 1) {
      that.getMessgaeList(2, token);
    }
    this.setData({
      //拿到当前索引并动态改变
      currentIndex: e.currentTarget.dataset.idx,
      orderList: orderList
    })
  },
  // 获取消息列表
  getMessgaeList: function (type, token) {
    var that = this;
    if (type == 1) {
      api._get('/msg/list', '', token).then(res => {
        if (res.code == 200) {
          var list = res.result;
          list.forEach(function (value, index) {
            var createTime = value.createTime * 1000;
            list[index]['showCreateTime'] = app.formatDate(createTime, 'YYYY-MM-DD H:m:s');
          });
          that.setData({
            messageList: list
          });
        } else if (res.code > 400) {
          wx.navigateTo({
            url: '../login/login',
          })
        } else {
          wx.showToast({
            title: '网络忙请重试',
            icon: 'none',
          })
        }
      })
    } else if (type == 2) {
      api._get('/msg/sys/list', '', token).then(res => {
        if (res.code == 200) {
          var list = res.result;
          list.forEach(function (value, index) {
            var createTime = value.createTime * 1000;
            list[index]['showCreateTime'] = app.formatDate(createTime, 'YYYY-MM-DD H:m:s');
          });
          that.setData({
            messageList: list
          });
        }
      })
    }

  },
  detail: function (e) {
    var id = e.currentTarget.dataset.messageid
    var that = this
    wx.navigateTo({
      // url: '/pages/message/detail/index?id=' + id + '&type=' + that.data.currentIndex,
      url: 'detail/index?id=' + id + '&type=' + that.data.currentIndex,
    })
  },
  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function (res) {
    console.log(res);
    if (res.from === 'button') {
      console.log('来自页面内转发按钮');
      wx.switchTab({
        url: '/pages/index/index'
      })
      this.setData({
        showShareModelStatus: false
      })
    } else if (res.from === 'menu') {
      console.log(res.from);
      console.log('右上角菜单转发按钮');
    }
  }
})